"""
File: example_1.py
Project: example_1
File Created: 2020/02/11 21:03:35
Author: Dunmore (fovd2020@outlook.com)
-----------------------------------------------------------
Description:
1.2 思考与练习 基础部分 1.录入班级成员档案
输入班级成员档案信息，运行程序后档案信息以Excel表格的形式保存
-----------------------------------------------------------
"""
import os
import xlwt

# 获取文件所在的路径
CURRRENT_DIR = (
    os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()
)


def get_member_info():
    """获取输入的班级成员信息

    Returns:
        dict: 包含班级成员的学号、姓名、年龄、生日、爱好
    """
    student_number = input("请输入学号：")
    name = input("请输入姓名：")
    age = input("请输入年龄：")
    birthday = input("请输入生日(如20070101)：")
    hobby = input("请输入爱好：")
    return {
        "student_number": student_number,
        "name": name,
        "age": age,
        "birthday": birthday,
        "hobby": hobby,
    }


def write_to_excel(data, file_path):
    """将装有班级成员信息的data写入到file_path指定的Excel文件中去

    Args:
        data (list): 班级成员信息
        file_path (str): 将要写入的Excel文件路径
    """
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet("Sheet1")

    # 在第一行写入标题
    sheet.write(0, 0, "学号")
    sheet.write(0, 1, "姓名")
    sheet.write(0, 2, "年龄")
    sheet.write(0, 3, "生日")
    sheet.write(0, 4, "爱好")

    for row, d in enumerate(data, start=1):
        sheet.write(row, 0, d["student_number"])
        sheet.write(row, 1, d["name"])
        sheet.write(row, 2, d["age"])
        sheet.write(row, 3, d["birthday"])
        sheet.write(row, 4, d["hobby"])

    workbook.save(file_path)


def main():
    class_member = []
    while True:
        print("====================班级档案管理系统====================")
        is_continue = input("是否继续输入班级成员档案（Y继续/其他任意键退出）:")
        if is_continue not in ["Y", "y"]:
            print("-------------------------退出---------------------------")
            break
        class_member.append(get_member_info())

    # 将输入的数据通过xlwt写入Excel文件
    write_to_excel(class_member, os.path.join(CURRRENT_DIR, "班级成员档案.xls"))


if __name__ == "__main__":
    main()
